<?php

namespace app\admin\controller\evaluation;

use app\admin\model\EvaluationQrcode;
use app\admin\model\SystemAdmin;
use app\admin\model\SystemConfig;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\Request;

/**
 * @ControllerAnnotation(title="evaluation_template")
 */
class Template extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\EvaluationTemplate();
        
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
//                ->with(['systemAdmin'])
                ->where($where)
                ->count();
            $list = $this->model
//                ->with(['systemAdmin'])
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();


            $info = $this->model->where('name',$post['name'])->find();
            if($info) $this->error('模板名称已存在');
            unset($post['star_name']);
            if($post['star_names']){
                $star_names = explode(',',$post['star_names']);
            }
            $post['star_names'] = $star_names;

            foreach($star_names as $k=>$v){
                if($k == 0){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][0]) ? 'on' : 'off',
                        'images' => isset($post['fields'][1]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][2]) ? 'on' : 'off',
                     ];
                }elseif($k == 1){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][3]) ? 'on' : 'off',
                        'images' => isset($post['fields'][4]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][5]) ? 'on' : 'off',
                    ];
                }elseif($k == 2){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][6]) ? 'on' : 'off',
                        'images' => isset($post['fields'][7]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][8]) ? 'on' : 'off',
                    ];
                }elseif($k == 3){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][9]) ? 'on' : 'off',
                        'images' => isset($post['fields'][10]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][11]) ? 'on' : 'off',
                    ];
                }elseif($k == 4){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][12]) ? 'on' : 'off',
                        'images' => isset($post['fields'][13]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][14]) ? 'on' : 'off',
                    ];
                }elseif($k == 5){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][15]) ? 'on' : 'off',
                        'images' => isset($post['fields'][16]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][17]) ? 'on' : 'off',
                    ];
                }elseif($k == 6){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][18]) ? 'on' : 'off',
                        'images' => isset($post['fields'][19]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][20]) ? 'on' : 'off',
                    ];
                }elseif($k == 7){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][21]) ? 'on' : 'off',
                        'images' => isset($post['fields'][22]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][23]) ? 'on' : 'off',
                    ];
                }elseif($k == 8){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][24]) ? 'on' : 'off',
                        'images' => isset($post['fields'][25]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][26]) ? 'on' : 'off',
                    ];
                }elseif($k == 9){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][27]) ? 'on' : 'off',
                        'images' => isset($post['fields'][28]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][29]) ? 'on' : 'off',
                    ];
                }elseif($k == 10){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][30]) ? 'on' : 'off',
                        'images' => isset($post['fields'][31]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][32]) ? 'on' : 'off',
                    ];
                }elseif($k == 11){
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][33]) ? 'on' : 'off',
                        'images' => isset($post['fields'][34]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][35]) ? 'on' : 'off',
                    ];
                }

            }

            $post['fields'] = $arr;
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $admin_id = session('admin.id');
            $userInfo = SystemAdmin::where('id',$admin_id)->find();
            if($userInfo['auth_ids'] == 8 || $userInfo['auth_ids'] == 7){
                $this->error('修改权限不足');
            }
            $post = $this->request->post();
            $t_info = $this->model->where('name',$post['name'])->find();
            if($t_info['id'] != $id){
                $this->error('模板名称已存在');
            }
            unset($post['star_name']);
            if($post['star_names']){
                $star_names = explode(',',$post['star_names']);
            }
            $post['star_names'] = $star_names;

            foreach($star_names as $k=>$v) {
                if ($k == 0) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][0]) ? 'on' : 'off',
                        'images' => isset($post['fields'][1]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][2]) ? 'on' : 'off',
                    ];
                } elseif ($k == 1) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][3]) ? 'on' : 'off',
                        'images' => isset($post['fields'][4]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][5]) ? 'on' : 'off',
                    ];
                } elseif ($k == 2) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][6]) ? 'on' : 'off',
                        'images' => isset($post['fields'][7]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][8]) ? 'on' : 'off',
                    ];
                } elseif ($k == 3) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][9]) ? 'on' : 'off',
                        'images' => isset($post['fields'][10]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][11]) ? 'on' : 'off',
                    ];
                } elseif ($k == 4) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][12]) ? 'on' : 'off',
                        'images' => isset($post['fields'][13]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][14]) ? 'on' : 'off',
                    ];
                } elseif ($k == 5) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][15]) ? 'on' : 'off',
                        'images' => isset($post['fields'][16]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][17]) ? 'on' : 'off',
                    ];
                } elseif ($k == 6) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][18]) ? 'on' : 'off',
                        'images' => isset($post['fields'][19]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][20]) ? 'on' : 'off',
                    ];
                } elseif ($k == 7) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][21]) ? 'on' : 'off',
                        'images' => isset($post['fields'][22]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][23]) ? 'on' : 'off',
                    ];
                } elseif ($k == 8) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][24]) ? 'on' : 'off',
                        'images' => isset($post['fields'][25]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][26]) ? 'on' : 'off',
                    ];
                } elseif ($k == 9) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][27]) ? 'on' : 'off',
                        'images' => isset($post['fields'][28]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][29]) ? 'on' : 'off',
                    ];
                } elseif ($k == 10) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][30]) ? 'on' : 'off',
                        'images' => isset($post['fields'][31]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][32]) ? 'on' : 'off',
                    ];
                } elseif ($k == 11) {
                    $arr[$k] = [
                        'star_name' => $v,
                        'content' => isset($post['fields'][33]) ? 'on' : 'off',
                        'images' => isset($post['fields'][34]) ? 'on' : 'off',
                        'isanonymous' => isset($post['fields'][35]) ? 'on' : 'off',
                    ];
                }
            }
            $post['fields'] = $arr;
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="生成二维码")
     */
    public function setQrcode($id)
    {
        $row = $this->model->find($id);

        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();

            if(!$post['student_arr']){
                $this->error('请上传班级学生姓名');
            }
            $post['student_arr'] = explode(',',$post['student_arr']);
            unset($post['file']);
            $post['template_id'] = $id;
            $post['qr_user_id'] = session('admin.id');
            $rule = [];
            $this->validate($post, $rule);

            try {
                $model = new EvaluationQrcode();
                $post['create_time'] = time();
                $post['update_time'] = time();
                $qr_id = $model->insertGetId($post);
                $content = '班级名称:' .$post['class_name']. PHP_EOL .'班级描述：' . $post['class_desc'] . PHP_EOL . '上课时间：' .$post['class_time']. PHP_EOL. '截至时间：' .$post['evaluate_period_time'];
                list($qr_code,$value) = getCode($id,$qr_id,$content);

                $up_post['qr_code'] = $qr_code;
                $up_post['link_address'] = $value;

                $save = $model->where('id',$qr_id)->update($up_post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }


    /**
     * @NodeAnotation(title="查看二维码")
     */
//    public function order($id)
//    {
//        $row = $this->model->find($id);
//
//        empty($row) && $this->error('数据不存在');
//        if ($this->request->isAjax()) {
//            if (input('selectFields')) {
//                return $this->selectList();
//            }
//            list($page, $limit, $where) = $this->buildTableParames();
//            $count = EvaluationQrcode::where($where)
//                ->count();
//            $list = EvaluationQrcode::where($where)
//                ->page($page, $limit)
//                ->order($this->sort)
//                ->select();
//            $data = [
//                'code'  => 0,
//                'msg'   => '',
//                'count' => $count,
//                'data'  => $list,
//            ];
//            return json($data);
//        }
//        return $this->fetch();
//    }
}